﻿
namespace ECommerce.Controls
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	public partial class WaterWheelOnHomePage : System.Web.UI.UserControl
	{
		private Service.IProductService _ProductService;

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();
		}

		protected void RepeaterImgWheel_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null)
			{
				var HyperLinkProdPic = e.Item.FindControl("HyperLinkProdPic") as System.Web.UI.WebControls.HyperLink;

				var dateItem = e.Item.DataItem as Data.Entity.Product;

				HyperLinkProdPic.ImageUrl = "~/PictureHandler.ashx?picturesize=520&productid=" + dateItem.ProductID.ToString();
				HyperLinkProdPic.NavigateUrl = "~/ProductDetail.aspx?productid=" + dateItem.ProductID.ToString();
			}
		}

		private void BindData()
		{
			this.RepeaterImgWheel.DataSource = GetNew6Products();
			this.RepeaterImgWheel.DataBind();
		}

		private List<Data.Entity.Product> GetNew6Products()
		{
			if (SiteContext.Current != null && SiteContext.Current.WorkingLanguage != null)
			{
				int currentLanguageID = SiteContext.Current.WorkingLanguage.LanguageId;

				if (_ProductService == null)
				{
					_ProductService = new Service.ProductService();
				}

				var productByLang = _ProductService.GetProductsByLanguage(currentLanguageID);

				if (productByLang == null)
				{
					return null;
				}

				var prods = (from p in productByLang
							 where p.ShowOnHomePage
							 orderby p.DisplayOrder
							 select p).Take(6).ToList();

				return prods;
			}
			else
			{
				return null;
			}
		}
	}
}